package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class StudentServiceImpl extends BaseService implements IStudentService {

    @Override
    public List<StudentBean> findAll() {

        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> stuList = mapper.findAll();
        session.close();
        return stuList;
    }

    @Override
    public StudentBean findById(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean stu = mapper.findById(id);
        session.close();
        return stu;
    }

    @Override
    public List<StudentBean> findByName(String name) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> stuList = mapper.findByName(name);

        session.close();
        return stuList;
    }

    @Override
    public int add(StudentBean stu) {
        //得到会话对象
        SqlSession session = this.getSession();
        //获得持久层接口的实现类
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        //调用持久层的add（）方法
        mapper.add(stu);
        session.commit();
        session.close();

        return stu.getId();
    }

    @Override
    public void addList(List<StudentBean> stuList) {

        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.addList(stuList);

        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.del(id);

        session.commit();
        session.close();

    }

    @Override
    public void delMore(int[] idArray) {

        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delMore(idArray);

        session.commit();
        session.close();
    }

    @Override
    public void update(int id, LocalDate birthday) {

        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(id, birthday);

        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> stuList = mapper.findByItem(name, startDate, endDate);
        session.close();
        return stuList;
    }

    @Override
    public int countStu() {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        int sum = mapper.countStu();
        session.close();
        return sum;
    }

    @Override
    public CutPageBean<StudentBean> cutAll(int pageNo) {
        CutPageBean<StudentBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        //（页码-1）*每页显示的条数
        List<StudentBean> stuList = mapper.cutAll((pageNo - 1) * CutPageBean.PAGSIZE,
                CutPageBean.PAGSIZE);
        //传入显示信息的集合
        cutBean.setList(stuList);
        //得到总记录数
        cutBean.setCount(mapper.countStu());

        session.close();
        return cutBean;
    }

    @Override
    public CutPageBean<StudentBean> cutByItem(int pageNo, String name,
          LocalDate startDate, LocalDate endDate) {

        CutPageBean<StudentBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);

        List<StudentBean> stuList = mapper.cutByItem(
                (pageNo-1)*CutPageBean.PAGSIZE,
                CutPageBean.PAGSIZE,
                name,startDate,endDate);

        cutBean.setList(stuList);

        int count = mapper.cutByCount(name,startDate,endDate);
        cutBean.setCount(count);

        session.close();
        return cutBean;
    }
}
